const {
  app,
  BrowserWindow,
  protocol
} = require('electron')
const path = require('path')

const UseWebPack = false;
let win;

function createWindow() {
  win = new BrowserWindow({
    width: 800,
    height: 600,
    // contextIsolation:false,
    webPreferences: {
      preload: path.join(__dirname, '../renderer/preload.js'),
      contextIsolation: false,
      nodeIntegration: true, // 是否集成 Nodejs
      enableRemoteModule: true, //否则remote不能使用
      //nodeIntegrationInWorker: true,
      //nodeIntegrationInSubFrames: true,
    }
  })
  win.setProgressBar(0.5)
  if (UseWebPack) {
    webpack_LoadURL(win, 'index.html')
  } else {
    win.loadFile(path.join(__dirname, '../static/index.html'))
  }
}

function webpack_LoadURL(win, file) {
  let URL = require('url')
  let url = ''
  // 通过process.env.NODE_ENV判断当前代码是否在生产环境或开发环境运行
  if (process.env.NODE_ENV != 'production') {
    url = 'http://loaclhost:' + process.env.ELECTRON_WEBPACK_WDS_PORT
  } else {
    url = new URL(path.join(__dirname, file))
    url.protocol = 'file'
  }
  win.loadFile(url)
}

app.whenReady()
  .then(() => {
    console.log("app.whenReady.then")
    createWindow()
    win.on('close', event => {
      event.preventDefault();
    })
    app.on('activate', () => {
      if (BrowserWindow.getAllWindows().length === 0) {
        createWindow()
      }
    })
  })

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

let windowsTxt =
  `{
  "windows":[
    {"key":"Electron窗体","value": "AboutWindow.html"},
    {"key":"Electron不规则窗体","value":""},
    {"key":"Electron窗体控制","value":""},
    {"key":"托盘图标  与其他","value":""}
  ]
}`
/*
    {"key":"页面内容","value":""},
    {"key":"页面容器","value":""},
    {"key":"脚本注入","value":""},
    {"key":"页面动效","value":""},
    {"key":"本  地数据持久化","value":""},
    {"key":"浏览器数据持久化","value":""},
    {"key":"Sqlite数据持久化","value":""},
    {"key":"系统  对话框","value":""},
    {"key":"菜单与快捷键","value":""},
    {"key":"剪切板与系统通知","value":""},
*/
let windowsObj = null;

function openWindow(key) {
  if (key == null || key == undefined || key == "")
    return
  if (windowsObj == null) {
    windowsObj = JSON.parse(windowsTxt)
  }
  var file = null
  for (const kv of windowsObj.windows) {
    if (kv.key == key) {
      file = kv.value
      break
    }
  }
  if (file == null)
    return
  var file = windowsObj.windows[key]
  var winx = new BrowserWindow({
    width: 800,
    height: 600,
    frame: false,
    webPreferences: {
      contextIsolation: false,
      nodeIntegration: true,
    }
  })
  winx.loadFile(new URL(path.join(__dirname, '../static', file.toString())))
  winx.show()
  winx.on('ready-to-show', () => winx.setTitle(key))
  winx.on('close', () => {
    winx = null
  })
}

//IPC 主进程
const {
  ipcMain
} = require('electron')
ipcMain.on('openWindow', (event, obj) => {
  console.log(event.sender + obj)
  openWindow(obj)
})
ipcMain.on('winDestroy', event => {
  win.destroy()
})
ipcMain.on('appQuit', event => {
  app.quit()
})
ipcMain.on('procExit', event => {
  process.exit()
})